/**
  ******************************************************************************
  * @file HT8_EEPROM.c
  * @brief This file provides all the EEPROM firmware functions.
  * @author Holtek Semiconductor Inc.
  * @version V1.0.0
  * @date 2018-12-20
  ******************************************************************************
  * @attention
  *
  * Firmware Disclaimer Information
  *
  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
  *    other intellectual property laws.
  *
  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
  *    other than HOLTEK and the customer.
  *
  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
  *
  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
  ************************************************************************************************************/

#include "MCU_include.h"
/**
  * @brief EEPROM write Byte function.
  * @param[in] The data you want to write to EEPROM.
  * It can be 0x00~0xff.
  * @param[in] Specifies EEPROM address.
  * It can be 0x00~0x3f.
  * @retval None
  */
void EEPROM_WriteByte(u8 u8_Data,u8 adr)
{
	u8 TempEMI;
	
	TempEMI = _emi;
	/*config EEPROM init*/
	_mp1l = 0x40;
	_mp1h = 0x01;
	
	/*config EEPROM address*/
	_eeal = adr;	
	/*Write data*/
	_eed = u8_Data;	
	_emi = 0;		//disable global interrupt	
	/*config EEPROM Write mode*/
	_iar1 |= 0x08;
	_iar1 |= 0x04;
	_emi  = TempEMI;		//enable global interrupt				
	/* waitting for EEPROM write finishing */
	while(_iar1 & 0x04);
	_iar1 = 0;
	
	return;
}

/**
  * @brief EEPROM write Word function.
  * @param[in] The data you want to write to EEPROM.
  * It can be 0x00~0xff.
  * @param[in] Specifies EEPROM address1.
  * @param[in] Specifies EEPROM address2.
  * It can be 0x00~0x3f.
  * @retval None
  */
void EEPROM_WriteWord(u16 u16_Data,u8 adrL,u8 adrH)	//EEPROM Write mode,No interrupt
{
	u8 TempEMI;	
	u8 u8_Data_L,u8_Data_H;
	u8_Data_L = u16_Data & 0x00ff;
	u8_Data_H = u16_Data >> 8;
	
	TempEMI = _emi;
	/*config EEPROM init*/	
	_mp1l = 0x40;
	_mp1h = 0x01;
	
	/*config EEPROM address*/
	_eeal = adrL;	
	/*Write data*/
	_eed = u8_Data_L;	
		
	/*config EEPROM Write mode*/
	_iar1 |= 0x08;
	_iar1 |= 0x04;
		
	/* waitting for EEPROM write finishing */
	while(_iar1 & 0x04);
//	_iar1 = 0;
	
	/*config EEPROM address*/
	_eeah = adrH;	
	/*Write data*/
	_eed = u8_Data_H;	
	_emi = 0;		//disable global interrupt	
	/*config EEPROM Write mode*/
	_iar1 |= 0x08;
	_iar1 |= 0x04;
	_emi  = TempEMI;		//enable global interrupt				
	/* waitting for EEPROM write finishing */
	while(_iar1 & 0x04);
	_iar1 = 0;	
	return;
}
	

/**
  * @brief EEPROM read Byte function.
  * @param[in] Specifies EEPROM address that you want to read.
  * It can be 0x00~0x3f.
  * @retval EEPROM data.
  */
u8 EEPROM_ReadByte(u8 adr)
{
	/* config EEPROM init */
	u8 u8_EP_Read_Data;
	_mp1l = 0x40;
	_mp1h = 0x01;
	
	/*config EEPROM address*/
	_eeal = adr;
	
	/*config EEPROM Read mode*/
	_iar1 |= 0x02;
	_iar1 |= 0x01;
	
	/* waitting for EEPROM Read finishing */
	while(_iar1 & 0x01);
	_iar1 = 0;
		
	u8_EP_Read_Data = _eed;		//Read data
	return u8_EP_Read_Data;			
}

/**
  * @brief EEPROM read Word function.
  * @param[in] Specifies EEPROM address1 that you want to read.
  * @param[in] Specifies EEPROM address2 that you want to read.
  * It can be 0x00~0x3f.
  * @retval EEPROM data.
  */
u16 EEPROM_ReadWord(u8 adrL,u8 adrH)
{
	u16 u16_ReadData;
	u8 u8_EP_Read_Data_L = 0xff,u8_EP_Read_Data_H = 0xff;
	/* config EEPROM init */
	
	_mp1l = 0x40;
	_mp1h = 0x01;
	
	/*config EEPROM address*/
	_eeal = adrL;
	
	/*config EEPROM Read mode*/
	_iar1 |= 0x02;
	_iar1 |= 0x01;
	
	/* waitting for EEPROM Read finishing */
	while(_iar1 & 0x01);
		
	u8_EP_Read_Data_L = _eed;		//Read data
	
	/*config EEPROM address*/
	_eeah = adrH;
	
	/*config EEPROM Read mode*/
	_iar1 |= 0x02;
	_iar1 |= 0x01;
	
	/* waitting for EEPROM Read finishing */
	while(_iar1 & 0x01);
	_iar1 = 0;
		
	u8_EP_Read_Data_H = _eed;		//Read data	

	u16_ReadData = u8_EP_Read_Data_H<<8|u8_EP_Read_Data_L;
	if (u16_ReadData > 5000)
	return 1000;
	else	
	return u16_ReadData;			
}


void PowerCalibrationData()
{
	V_Calibration_K  = EEPROM_ReadWord(V_K_ADRL,V_K_ADRH);
	V_Calibration_C  = EEPROM_ReadWord(V_C_ADRL,V_C_ADRH);
	I_Calibration_K1 = EEPROM_ReadWord(I_K1_ADRL,I_K1_ADRH);
	I_Calibration_C1 = EEPROM_ReadWord(I_C1_ADRL,I_C1_ADRH);
	I_Calibration_K2 = EEPROM_ReadWord(I_K2_ADRL,I_K2_ADRH);
	I_Calibration_C2 = EEPROM_ReadWord(I_C2_ADRL,I_C2_ADRH);
	
	V_Cal_C  = EEPROM_ReadByte(V_CAL_C_ADR);
	I_Cal_C1 = EEPROM_ReadByte(I_CAL_C1_ADR);
	I_Cal_C2 = EEPROM_ReadByte(I_CAL_C2_ADR);
	CurrentPoint1 = EEPROM_ReadWord(CURAD1_ADRL,CURAD1_ADRH);
	GCC_DELAY(1);
}

/******************* (C) COPYRIGHT 2018 Holtek Semiconductor Inc *****END OF FILE****/